package com.totoro.mybatis.mul_datasource.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * Created by Niki on 2018/8/15 11:58
 */
@Configuration
@MapperScan(basePackages = {"com.totoro.mybatis.mul_datasource.dao.school"}, sqlSessionTemplateRef = "schoolSqlSessionTemplate")
public class MybatisDB2Config {
    @Autowired
    @Qualifier("db2")
    private DataSource dataSource;

    @Bean
    @Qualifier("sqlSessionFactory2")
    public SqlSessionFactory sqlSessionFactory2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        return factoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate schoolSqlSessionTemplate(SqlSessionFactory sqlSessionFactory2) throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory2);
        return template;
    }

}
